/*
** action3.c for zappy in /home/guidet_m/TECK2/projets/zappy/server_zappy
** 
** Made by michael guidet
** Login   <guidet_m@epitech.net>
** 
** Started on  Mon Jul  4 15:40:44 2011 michael guidet
** Last update Mon Jul 11 10:14:43 2011 michael guidet
*/

#include "server.h"

void	action_connect(t_fd *fd, t_env *e, char *cmd)
{
  char	num_client[BUF_SIZE];
  int	i;

  printf("%sDrone %d %s Execute %s%s\n", PURPLE, fd->pl->id,
	 fd->pl->team, cmd, DEFAUT);
  fd->wait = -1;
  fd->idaction = -1;
  for (i = 0; e->teams[i].name; i++)
    if (!strcmp(e->teams[i].name, fd->pl->team))
      {
	sprintf(num_client, "%d\n", e->teams[i].dispo);
	break ;
      }
  add_string_to_list(&fd->send_list, num_client);
}

void	graph_newplayer(t_env *e, t_fd *fd)
{
  t_fd	*tmp;
  char	cmd2[BUF_SIZE];
  int	temppos;

  temppos = (fd->pl->look_at == NORD) ? 1 :
    (fd->pl->look_at == EST) ? 2 :
    (fd->pl->look_at == SUD) ? 3 : 4;
  sprintf(cmd2, "pnw %d %d %d %d %d %s\n", fd->pl->id, fd->pl->x,
	  fd->pl->y, temppos, fd->pl->level, fd->pl->team);
  for (tmp = e->fd; tmp; tmp = tmp->next)
    if (tmp->type == GRAPH)
      add_string_to_list(&tmp->send_list, cmd2);
}

void	player_join_team(t_fd *fd, t_team *team, t_env *e)
{
  char	num_client[BUF_SIZE];

  if (team->dispo <= 0)
    {
      x_int(-1, send(fd->fd, "ko\n", 3, 0), "send");
      remove_from_fdlist(&e->fd, fd);
      return ;
    }
  printf("%sUn joueur a join la team %s%s\n", PURPLE, team->name, DEFAUT);
  fd->wait = -1;
  fd->idaction = -1;
  fd->pl = team->ia_players->drone;
  fd->type = AI;
  remove_drone_from_team(team, fd->pl);
  team->dispo--;
  add_drone_to_case(e->map, fd->pl->x, fd->pl->y, fd->pl);
  sprintf(num_client, "%d\n%d %d\n", team->dispo, e->width, e->height);
  add_string_to_list(&fd->send_list, num_client);
  graph_newplayer(e, fd);
}

void		join_graph(t_fd *fd, t_env *e)
{
  char		cmd2[BUF_SIZE];

  printf("%sUn client graphique s'est connecte%s\n", PURPLE, DEFAUT);
  fd->type = GRAPH;
  sprintf(cmd2, "msz %d %d\n", e->width, e->height);
  add_string_to_list(&fd->send_list, cmd2);
  sprintf(cmd2, "sgt %d\n", e->t);
  add_string_to_list(&fd->send_list, cmd2);
  sendbct(fd, e);
  sendtna(fd, e);
  sendpnw(fd, e);
  sendenw(fd, e);
}
